package com.immediatus.engine.util;

import com.immediatus.contracts.IUpdateHandler;

import java.util.ArrayList;

public class RunnableHandler implements IUpdateHandler{
    private final ArrayList<Runnable> _runnables = new ArrayList<Runnable>();

    @Override
    public synchronized void onUpdate(final float secondsElapsed_){
        final ArrayList<Runnable> runnables = this._runnables;
        final int runnableCount = runnables.size();
        for (int i = runnableCount - 1; i >= 0; i--){
            runnables.get(i).run();
        }
        runnables.clear();
    }

    @Override
    public void reset(){
        this._runnables.clear();
    }

    public synchronized void postRunnable(final Runnable runnable_){
        this._runnables.add(runnable_);
    }
}
